package bank_access;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Logger;

public class ManagerSkeletonTask implements Runnable {

    private Socket socket;
    private ManagerImplBase mib;
    private Logger logger;

    public ManagerSkeletonTask(ManagerImplBase mib, Socket socket) {
        logger = Kommunikationsmodul.getLogger();
        this.socket = socket;
        this.mib = mib;
    }

    public void run() {

        try {
            BufferedReader dataIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());
            String message = dataIn.readLine();
            String hostName = socket.getInetAddress().getCanonicalHostName();
            logger.info("Nachricht von " + hostName + ":" + socket.getPort() + ": " + message);

            String[] params = message.split("#");
            String command = params[1];
            //Nachricht ist z.B. "CALL#KONTOEINRICHTEN#**\n";
            if (command.equals("KONTOEINRICHTEN")) {
                dataOut.writeBytes("OK#" + mib.kontoEinrichten() + "#**\n");
                logger.info("Nachricht an " + hostName + ":" + socket.getPort() + ": " + "OK#" + "KONTO_ID" + "#**\n");
            }

        } catch (IOException e) {
            logger.info("IOException " + e);
        }
    }
}
